$(function () {
    //请求参数封装的封装的对象
    var request_data = {}
    var find = {}

    //遍历列表

    getBrand();
/*    gettype();*/

    //接收一个值

    var findtype = $('.input-find').val()
    if (findtype != null && findtype != ''){
        find.typename = findtype;
        getList(find)
    }else{
        getList(request_data)
    }

    $('#search-input').keydown(function (e) {
        if (e.keyCode===13) {
            console.log("回车事见被触发~")
            var type = $('#search-input').val()
            request_data.typename = type;
            getList(request_data)
        }
    })
    //遍历筛选品牌
    function getBrand(data) {
        data = data===undefined?{}:data
        $.ajax({
            url:'/getBrandnameList',
            type:'POST',
            cache:'false',
            datatype:'json',
            contentType:'application/json;charset=utf-8',
            data:JSON.stringify(data),
            success:function (data) {
                console.log("data",data)
                if (data.success){
                    //做渲染
                    // alert(data);
                    brandnameList(data.data)
                }else {
                    alert("品牌筛选加载失败");
                }
            }
        })
    }
    function brandnameList(data) {
        var html = '<li class="active"><a data-id="'+(0)+'" class="pin-pai" href="javascript:;">不限</a></li>';
        data.map(function (item,index) {
            //alert(item.bid);
            html += showBrand(item.bid,item.bname)
        })
        $("#brandname-list").html(html)
    }
    function showBrand(bid,bname) {
        return '<li ><a data-id='+bid+' class="pin-pai" href="javascript:;">'+bname+'</a></li>'
    }




    //定位到选择的品牌    ,status:status
    $("#brandname-list").on('click','a',function (e) {
        console.log("a标签被点击了")
        var target = $(e.currentTarget)
        if (target.hasClass('pin-pai')){
            console.log("定位到品牌的a标签~")
            var bandid = e.currentTarget.dataset.id
            console.log("id:===="+bandid)
            if(bandid===0){
                getList();
            }
            $.ajax({
                url:'/togetType?bandid='+bandid,
                type:'POST',
                cache: false,
                contentType:'application/json;charset=utf-8',
                success:function (data) {
                    if (data.success){
                       $(".search-filter .sub-filter .filter-list ul li").on('click', function() {
                            $(this).parent().find("li").removeClass("active")
                            $(this).addClass("active");
                        });
                        typeList(data.data);
                        request_data.bandid = bandid;
                        getList(request_data);
                        console.log(data)
                    }else{
                       alert("获取失败");
                    }
                }
            })
        }
    });
